//
//  HMOAuthViewController.swift
//  webo
//
//  Created by apple on 2016/11/15.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SVProgressHUD
class HMOAuthViewController: UIViewController {

    let webView = UIWebView()

    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(close))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "直达", style: .plain, target: self, action: #selector(directionEnter))
        
        loadOAuthPage()

    }
   @objc private func directionEnter(){
    let jsString = "document.getElementById('userId').value = '18858660584', document.getElementById('passwd').value = 'w199146'"
    webView.stringByEvaluatingJavaScript(from: jsString)
    }
   @objc private func close(){
        dismiss(animated: true, completion: nil)
    }
    
   private func loadOAuthPage(){
    
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
    
//    + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
    
        let url = URL(string: urlString)
    
        let request = URLRequest(url: url!)
    
        webView.loadRequest(request)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        SVProgressHUD.dismiss()
    }
    
    //死了
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}



extension HMOAuthViewController: UIWebViewDelegate{

    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //        print(request)
        //        http://www.itheima.com/?code=8da11fe66c490d5664d117581fe5bf39
        let urlstring = request.url?.absoluteString ?? ""
        let flag = "code="
        //如果获取了code之后就应该 return false 不加载页面
        if urlstring.contains(flag) {
            //            获取字符串
            let query = request.url?.query ?? ""
//            print("query",query)
            let code = (query as NSString).substring(from: flag.characters.count)
            print(code)
            HMUserAccountViewModel.shared.loadAccuessTocken(code: code, finished: { (result: Bool) -> () in
                print(result)
                if !result{
                    
                SVProgressHUD.showError(withStatus: "没有成功")
                    
                    return
                }
//                print("请求成功")
//                UIApplication.shared.keyWindow?.rootViewController = HMWelcomeViewController()
                NotificationCenter.default.post(name: NSNotification.Name(chooseNotificationLoginOn), object: "welcome")
            })
            
            return false
        }
        
    return true
    
    }

    
}




